package cp7_jstl_javabean.user;

import util.DBTool;
import util.StringUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;

@WebServlet("/cp7/user/delete")
public class DeleteUserServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 获取要删除的数据的id
        String id = req.getParameter("id");
        // 构建删除的sql
        String sql = "delete from userinfo where id=" + id;
        PrintWriter out = StringUtil.getWriter(resp);
        // 执行参数
        try {
            int count = DBTool.executeUpdate(sql);
            if (count == 0) {
                out.println("删除失败");
            } else {
                // 删除成功 重新回到列表页
                resp.sendRedirect("/cp7/user/list");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
